import axios from 'axios';
import { message } from 'ant-design-vue';
export const httpRequest = (
  url,
  params,
  method,
  headers = 'application/x-www-form-urlencoded'
) => {
  return new Promise((resolve, reject) => {
    axios({
      method: method,
      url: url,
      params: method != 'post' ? params : '',
      data: method == 'post' ? params : '',
      headers: {
        token: JSON.parse(localStorage.getItem('token')),
        'conten-type': headers,
      },
      timeout: 10000,
    })
      .then((res) => {
        if (res.data?.status) {
          res.data.status == 200 ? resolve(res.data) : reject(res.data);
        } else {
          resolve(res.data);
        }
      })
      .catch((err) => {
        if (err.response) {
          let errMessage = err.response.data.message
            ? err.response.data.message + err.response.data.status
            : err.response.data.error + err.response.data.status;
          message.error(errMessage);
          err.response.data.status == 401
            ? window.parent.postMessage(err.response.data, '*')
            : '';
          reject(err.response.data);
        } else {
          message.error(err.message);
          reject(err);
        }
      });
  });
};
